package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author shangguanzhiyu
 * @date 2025-08-07
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanVoById(Long id)
    {
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        // 根据护理计划id查询护理计划项目关联表
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.selectNursingProjectPlanVoById(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        // 1.新增护理计划
        NursingPlan nursingPlan = new NursingPlan();
        // 当两类的舒心名相同且类型兼容的情况下,可以使用舒心拷贝的方式快速构造实体
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        int row = nursingPlanMapper.insert(nursingPlan);
        if(row <= 0){
            throw new RuntimeException("新增护理计划失败");
        }
        // 2.批量新增护理计划与护理项目的关联关系
        nursingPlanDto.getProjectPlans()
                .forEach(projectPlan ->{
                    projectPlan.setPlanId(nursingPlan.getId());
                });
        row = nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans());
        if (row <= 0){
            throw new RuntimeException("新增护理计划与护理项目的关联关系失败");
        }
        return 1;

    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto){
        // 1.修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        int row = nursingPlanMapper.updateById(nursingPlan);
        if (row <= 0){
            throw new RuntimeException("修改护理计划失败");
        }
        // 2.批量修改护理计划与护理项目的关联关系
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(StringUtils.isNotEmpty(projectPlans)){
            // 2.1 先删除
            Long planId = nursingPlanDto.getId();
            row = nursingProjectPlanMapper.deleteByPlanId(planId);
            if(row <= 0){
                throw new RuntimeException("修改护理计划与护理项目的关联关系失败【删除失败】");
            }
            // 2.2 再新增
            projectPlans.forEach(projectPlan ->{projectPlan.setProjectId(planId);});
            row = nursingProjectPlanMapper.batchInsert(projectPlans);
            if(row <= 0){
                throw new RuntimeException("修改护理计划与护理项目的关联关系失败【新增失败】");
            }
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return nursingPlanMapper.deleteNursingPlanByIds(ids) ;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        int row = nursingPlanMapper.deleteById(id);
        if(row <= 0){
            throw new RuntimeException("删除护理计划失败");
        }
        try{
            nursingProjectPlanMapper.deleteByPlanId(id);
        }catch (Exception e){
            throw new RuntimeException("删除护理计划与护理项目的关联关系失败");
        }
        return 1;
    }

    @Override
    public List<NursingPlan> listAll() {
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus ,1);
        return this.list(queryWrapper);
    }
}
